package com.mall.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.entity.TbGoods;
import com.mall.entity.TbGoodsCarousel;
import com.mall.entity.TbItmeSpec;
import com.mall.product.mapper.GoodsCarouselMapper;
import com.mall.product.mapper.GoodsMapper;
import com.mall.product.mapper.ItemSpecMapper;
import com.mall.product.service.IGoodsService;
import com.mall.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * 商品服务impl
 * created with IntelliJ IDEA
 *
 * @author ASUS
 * @Auther: W
 * @Date: 2021/07/16/11:01
 * Other:
 * @date 2021/07/18
 */
@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsCarouselMapper goodsCarouselMapper;

    @Autowired
    private ItemSpecMapper itemSpecMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    static IdWorker idWorker = new IdWorker();

    /**
     *
     * @param goods 货物
     * @return Long 主键id
     */
    @Override
    public Long addGoods(@RequestBody TbGoods goods) {
        long nextId = idWorker.nextId();
        goods.setId(nextId);
        goodsMapper.addGoods(goods);
        // 添加商品轮播图
        List<String> images = JSON.parseArray(goods.getImages(), String.class);
        if (images != null) {
            images.stream().forEach(image ->{
                TbGoodsCarousel carousel = new TbGoodsCarousel(idWorker.nextId(),nextId,image);
                goodsCarouselMapper.insertImage(carousel);
            });
        }
        return nextId;
    }

    @Override
    public List<TbGoods> findAll() {
        return goodsMapper.findAll();
    }

    @Override
    public PageInfo findList(int page, int size) {
        PageHelper.startPage(page,size);
        List<TbGoods> goodsList = goodsMapper.findList();
        return new PageInfo(goodsList);
    }

    /**
     * 将分类商品信息存入缓存
     * @param typeId id类型
     * @return
     */
    @Override
    @Cacheable(cacheNames = {"goodsByTypeId"})
    public List<TbGoods> findListByTypeId(Integer typeId) {
        return goodsMapper.findListByTypeId(typeId);
    }

    /**
     * 从缓存中获取秒杀商品信息展示到用户前台
     * @param time 当前时间
     * @return 商品列表
     */
    @Override
    public List<TbGoods> findSeckillList(String time) {
        List<TbGoods> goods = (List<TbGoods>) redisTemplate.opsForValue().get("seckillGoods");
        if (goods==null){
            List<TbGoods> list = goodsMapper.findSeckillList(time);
            redisTemplate.opsForValue().set("seckillGoods",list);
            return list;
        }
        return goods;
    }



    /**
     * 根据商品id查询信息
     * @param id
     * @return
     */
    @Override
    public TbGoods findById(Long id) {
        TbGoods goods = goodsMapper.findById(id);
        List<TbItmeSpec> byGoodsId = itemSpecMapper.findByGoodsId(id);
        if (byGoodsId != null && byGoodsId.size() >0) {
            goods.setItmeSpecList(byGoodsId);
        }
        return goods;
    }



}
